package cn.org.timespace.easyframe.action.permission;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfMenu;
import cn.org.timespace.easyframe.service.permission.EfMenuService;
import cn.org.timespace.easyframe.util.bean.BeanUtil;

/**
 * <p>
 * 菜单
 * 
 * @author wubo
 *         </p>
 */
public class EFMenuAction extends EFBaseAction {

	private EfMenuService efMenuService;

	// 构建模型驱动bean
	public EFMenuAction() {
		super(new EFMenuModel());
	}

	public EfMenuService getEfMenuService() {
		return efMenuService;
	}

	public void setEfMenuService(EfMenuService efMenuService) {
		this.efMenuService = efMenuService;
	}

	/**
	 * 查询所有菜单
	 * 
	 * @return
	 */
	public String doList() {
		// 得到一级菜单
		List topMenuList = efMenuService.findTopLevelMenu();
		getRequest().setAttribute("topMenuList", topMenuList);

		// 得到二级菜单
		List secondLevelMenu = efMenuService.findSecondLevelMenu();
		getRequest().setAttribute("secondLevelMenu", secondLevelMenu);

		// 得到多有菜单
		List AllMenuList = efMenuService.findByCondition(getEfMenuModel());
		getRequest().setAttribute(this.VOLIST, AllMenuList);
		return LIST;
	}

	// 添加菜单
	public String doSave() {
		
		efMenuService.save(getEfMenuModel().getEfMenu());
		
		return UPDATE;
	}

	// 修改菜单定义
	public String doUpdate() {

		EfMenu efMenu = getEfMenuModel().getEfMenu();
		efMenuService.update(efMenu);

		return UPDATE;
	}

	// 根据id删除一个菜单
	public String doDelete() {
		EfMenu efMenu = getEfMenuModel().getEfMenu();
		efMenuService.delete(efMenu);
		return "delete";
	}

	// 根据id查找某个菜单
	public String doFindById() {
		
		//更新
		String menuId = getEfMenuModel().getMenuId();
		if (!StringUtils.isEmpty(menuId)) {
			List<EfMenu> resList = efMenuService.findByCondition(getEfMenuModel());
			if (resList != null && resList.size() == 1) {
				getEfMenuModel().setEfMenu(resList.get(0));
			}
			return UPDATE;
		}
		
		//增加
		String parentMenuId = getRequest().getParameter("parentMenuId");
			if(!StringUtils.isEmpty(parentMenuId)){
			EfMenu efMenu = getEfMenuModel().getEfMenu();
			String parentMenuName = efMenu.getMenuName();
			BeanUtil.beanProperty2Null(efMenu);
			
			efMenu.setParentMenuId(parentMenuId);
			getEfMenuModel().setEfMenu(efMenu);
			getEfMenuModel().setParentMenuName(parentMenuName);
			
			return ADD;
		}

		return null;
	}
	
	private EFMenuModel getEfMenuModel() {
		return (EFMenuModel) getModel();
	}
}
